package org.quartzweb.util;

import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/**
 * jdbc工具类 mysql版
 * Created by Administrator on 2016/12/30.
 */
public class JdbcUtils {

    static String url = "jdbc:mysql://127.0.0.1:3306/quartz1?useSSL=true";
    static String username = "root";
    static String password = "root";
    static Connection con = null;
    static PreparedStatement ps = null;
    static ResultSet rs = null;

    public static void main(String[] args) throws SQLException {
        //List<Map<String, String>> triggerInfos = JdbcUtils.query("SELECT * FROM QUARTZ1.QRTZ_TRIGGERS");

        System.out.println(new Date(System.currentTimeMillis()).toLocaleString());
        System.out.println(new Date(Long.parseLong("1483069260000")).toLocaleString());

        /*for(Map<String, String> triggerInfo : triggerInfos){
            for (Map.Entry<String, String> entry : triggerInfo.entrySet()) {
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();
                System.out.println("key=" + key + " value=" + value);
            }
        }*/
    }

    /**
     * 查询
     * @param sql sql语句
     * */
    public static List<Map<String, String>> query(String sql) {
        List<Map<String, String>> triggerInfos = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection(url, username, password);
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                Map<String, String> triggerInfo = new LinkedHashMap<>();
                String job_name = rs.getString("JOB_NAME");
                String job_group = rs.getString("JOB_GROUP");
                String trigger_name = rs.getString("TRIGGER_NAME");
                String trigger_group = rs.getString("TRIGGER_GROUP");
                String trigger_state = rs.getString("TRIGGER_STATE");
                String trigger_type = rs.getString("TRIGGER_TYPE");
                String start_time = rs.getString("START_TIME");
                String end_time = rs.getString("END_TIME");
                triggerInfo.put("job_name", job_name);
                triggerInfo.put("job_group", job_group);
                triggerInfo.put("trigger_name", trigger_name);
                triggerInfo.put("trigger_group", trigger_group);
                triggerInfo.put("trigger_state", trigger_state);
                triggerInfo.put("trigger_type", trigger_type);
                triggerInfo.put("start_time", new Date(Long.parseLong(start_time)).toLocaleString());
                triggerInfo.put("end_time", new Date(Long.parseLong(end_time)).toLocaleString());
                triggerInfos.add(triggerInfo);
            }
        } catch (Exception e) {
            System.out.print("MYSQL ERROR:" + e.getMessage());
        } finally {
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return triggerInfos;
    }
}
